@@ -0,0 +1,61 @@ |
||
1 |
+module Agents |
|
2 |
+ class EventFormattingAgent < Agent |
|
3 |
+ cannot_be_scheduled! |
|
4 |
+ |
|
5 |
+ description <<-MD |
|
6 |
+ Event Formatting Agent allows you to format your events in any way you prefer. |
|
7 |
+ For example if you have an event like |
|
8 |
+ |
|
9 |
+ { |
|
10 |
+ :content => { |
|
11 |
+ :text => "Lorem ipsum dolor sit amet" |
|
12 |
+ }, |
|
13 |
+ :fields => "consectetur adipisicing elit", |
|
14 |
+ :data => "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua." |
|
15 |
+ } |
|
16 |
+ |
|
17 |
+ |
|
18 |
+ And other agents which are receiving this event, say, Twilio Agent expects `message` key , and Digest Email Agent which expects a `subject` key, then you can reformat this event by filling `Options` in the following way. |
|
19 |
+ |
|
20 |
+ message => $.content.text |
|
21 |
+ subject => $.fields |
|
22 |
+ |
|
23 |
+ Values must be provided in JSONPath. You can add as many keys as you like. |
|
24 |
+ |
|
25 |
+ Then, event generated by Event Formatting Agent will be like |
|
26 |
+ |
|
27 |
+ { |
|
28 |
+ :message => "Lorem ipsum dolor sit amet" |
|
29 |
+ :subject => "consectetur adipisicing elit" |
|
30 |
+ } |
|
31 |
+ |
|
32 |
+ MD |
|
33 |
+ |
|
34 |
+ event_description <<-MD |
|
35 |
+ |
|
36 |
+ User defined |
|
37 |
+ MD |
|
38 |
+ |
|
39 |
+ def default_options |
|
40 |
+ { |
|
41 |
+ :message => "$.data.fields.content" |
|
42 |
+ } |
|
43 |
+ end |
|
44 |
+ |
|
45 |
+ def working? |
|
46 |
+ true |
|
47 |
+ end |
|
48 |
+ |
|
49 |
+ def receive(incoming_events) |
|
50 |
+ incoming_events.each do |event| |
|
51 |
+ formatted_event = {} |
|
52 |
+ options.each_pair do |key,value| |
|
53 |
+ formatted_event[key] = Utils.values_at event.payload,value |
|
54 |
+ end |
|
55 |
+ formatted_event[:agent] = Agent.find(event.agent_id).type.slice! 8..-1 |
|
56 |
+ formatted_event[:created_at] = event.created_at |
|
57 |
+ create_event :payload => formatted_event |
|
58 |
+ end |
|
59 |
+ end |
|
60 |
+ end |
|
61 |
+end |